﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ReprojectWebMercator
{
    public class vec3
    {
        public float x, y, z;

        #region 属性
        public override string ToString()
        {
            return x.ToString("f3") + "  " + y.ToString("f3") + " " + z.ToString("f3");
        }
        public vec2 xy { get { return new vec2(x, y); } }
        public vec2 yz { get { return new vec2(y, z); }
            set
            {
                y = value.x;
                z = value.y;
            }
        }
        public vec3 get3(string v)
        {
            return new vec3(getT(v[0]), getT(v[1]), getT(v[2]));
        }
        public vec4 get4(string v)
        {
            return new vec4(getT(v[0]), getT(v[1]), getT(v[2]), getT(v[3]));
        }
        private float getT(char t)
        {
            if (t == 'x') return x;
            if (t == 'y') return y;
            if (t == 'z') return z;
            return 0;
        }
        #endregion


        #region 构造函数
        public vec3() { }
        public vec3(vec2 u_textureDimensions, float v1)
        {
            this.x = u_textureDimensions.x;
            this.y = u_textureDimensions.y;
            this.z = v1;
        }
        public vec3(float v1)
        {
            this.x = v1;
            this.y = v1;
            this.z = v1;
        }
        public vec3(float v1, float v2, float v3)
        {
            this.x = v1;
            this.y = v2;
            this.z = v3;
        }
        public vec3(double v1, double v2, double v3)
        {
            this.x = (float)v1;
            this.y = (float)v2;
            this.z = (float)v3;
        }
        public vec3(string json)
        {
            var jvalue = JsonConvert.DeserializeObject<vec3>(json);
            x = jvalue.x;
            y = jvalue.y;
            z = jvalue.z;
        } 
        #endregion


        public vec3 Dot(vec3 other)
        {
            return new vec3(x * other.x, y * other.y, z * other.z);
        }

        public static vec3 operator +(vec3 v1, vec3 v2)
        {
            return new vec3(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);
        }
        public static vec3 operator +(vec3 v1, double v2)
        {
            return new vec3(v1.x + v2, v1.y + v2, v1.z + v2);
        }
        public static vec3 operator +(float v1, vec3 v2)
        {
            return new vec3(v1 + v2.x, v1 + v2.y, v1 + v2.z);
        }
        public static vec3 operator -(float v1, vec3 v2)
        {
            return new vec3(v1 - v2.x, v1 - v2.y, v1 - v2.z);
        }
        public static vec3 operator -(double v1, vec3 v2)
        {
            return new vec3(v1 - v2.x, v1 - v2.y, v1 - v2.z);
        }
        public static vec3 operator -(vec3 v1, double v2)
        {
            return new vec3(v1.x - v2, v1.y - v2, v1.z - v2);
        }
        public static vec3 operator -(vec3 v1, vec3 v2)
        {
            return new vec3(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
        }
        public static vec3 operator /(vec3 v1, float t)
        {
            return new vec3(v1.x/t, v1.y/t, v1.z/t);
        }
        public static vec3 operator *(float t, vec3 v1)
        {
            return new vec3(v1.x * t, v1.y * t, v1.z * t);
        }
        public static vec3 operator *(vec3 v1,float t )
        {
            return new vec3(v1.x * t, v1.y * t, v1.z * t);
        }
        public static vec3 operator *(vec3 v1, double t)
        {
            return new vec3(v1.x * t, v1.y * t, v1.z * t);
        }
        public static vec3 operator *(double t, vec3 v1)
        {
            return new vec3(v1.x * t, v1.y * t, v1.z * t);
        }
        public static vec3 operator *(vec3 t, vec3 v1)
        {
            return new vec3(v1.x * t.x, v1.y * t.y, v1.z * t.z);
        }
    }
}
